#include <QtTest>

#include "mpIoContextTest.h"

#include "mpConfig.h"
#include "mpFileUtils.h"
#include "mpLog.h"
#include "mpCommonTests.h"
#include "mpCodecUtilsTests.h"
#include "mpWin32FuncTests.h"
#include "mpDateUtilsTests.h"
#include "mpFileUtilsTests.h"
#include "mpLocaleUtilsTests.h"
#include "mpHashUtilsTests.h"
#include "mpLruCacheTests.h"
#include "mpStringUtilsTests.h"


#include "mpHttpUrlTests.h"
#include "mpHttpClitentTests.h"
#include "mpHttpConnectionManageTests.h"

//INITIALIZE_EASYLOGGINGPP

//#include <iostream>
int doTest(int index , const std::string & testName,QObject *testObject, int argc, char *argv[]){

    std::cout << "********* " << index << ". " << testName << " *********" << std::endl;
    int reuslt = QTest::qExec(testObject,argc,argv);
    std::cout << std::endl;
    return reuslt;
}

void do_io_context_test(){
    boost::asio::io_context io;
    //PrinterStrand p(io);
    Printer p(io),p1(io),p2(io),p3(io);

    std::thread t(boost::bind(&boost::asio::io_context::run, &io));
    //io.run();
    t.join();
}

void do_basic_unittests(int argc, char *argv[]){

    int status = 0;

    mpCommonTests commonTests;
    status |= doTest(0,"Common",&commonTests,argc,argv);

    mpCodecUtilsTests codecUtilsTests;
    status |= doTest(1,"base64 & des_ecb",&codecUtilsTests,argc,argv);

    mpWin32FuncTests win32FuncTests;
    status |= doTest(2,"Win32 API",&win32FuncTests,argc,argv);

    mpDateUtilsTests dateUtilsTests;
    status |= doTest(3,"DateTime",&dateUtilsTests,argc,argv);

    mpFileUtilsTests fileUtilsTests;
    status |= doTest(4,"FileOP",&fileUtilsTests,argc,argv);

    mpLocaleUtilsTests localeUtilsTests;
    status |= doTest(5,"Locale",&localeUtilsTests,argc,argv);

    mpLruCacheTests lruCacheTests;
    status |= doTest(6,"LruCache",&lruCacheTests,argc,argv);

    mpStringUtilsTests stringUtilsTests;
    status |= doTest(7,"String",&stringUtilsTests,argc,argv);

    mpHttpUrlTests httpUrlTests;
    status |= doTest(8,"HttpUrl",&httpUrlTests,argc,argv);

    mpHttpClitentTests httpClientTests;
    status |= doTest(9,"HttpClient",&httpClientTests,argc,argv);

    mpHttpConnectionManageTests httpConnectionManageTests;
    status |= doTest(10,"HttpConnectionManage",&httpConnectionManageTests,argc,argv);

    mpHashUtilsTests hashUtilsTests;
    status |= doTest(99,"Hash",&hashUtilsTests,argc,argv);

    MPLOG_INFO << "status :" << status;
}

int main(int argc, char *argv[])
{
    //初始化日志组件
    std::string logWorkDir = MP::FileUtils::get_current_userdata_dir() + PATH_DELIM + "logs";
    MP::Log::InitLog("mpTests",logWorkDir);

#if (QT_VERSION < QT_VERSION_CHECK(5,0,0))

#if _MSC_VER
    QTextCodec *codec = QTextCodec::codecForName("gbk");
#else
    QTextCodec *codec = QTextCodec::codecForName("utf-8");
#endif //_MSC_VER

    QTextCodec::setCodecForLocale(codec);
    QTextCodec::setCodecForCStrings(codec);
    QTextCodec::setCodecForTr(codec);

#else
    QTextCodec *codec = QTextCodec::codecForName("utf-8");
    QTextCodec::setCodecForLocale(codec);
#endif //QT_VERSION < QT_VERSION_CHECK(5,0,0)


    MPLOG_INFO << "maple unit tests starting ...";
    MPLOG_INFO << "main thread id :" << std::this_thread::get_id();

#ifdef TEST_STRATEGY_ALL
    MPLOG_INFO << "run all test script ...";
#else

#ifdef TEST_STRATEGY_COMPLETED
    MPLOG_INFO << "run completed test script ...";
#endif

#ifdef TEST_STRATEGY_CURRENT
    MPLOG_INFO << "run current test script ...";
#endif

#endif

    //do_io_context_test();

    do_basic_unittests(argc,argv);

    return 0;
}
